﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using FwProject.ShoppingMall.Keyword.Dto;

namespace FwProject.AppManager
{
    public class KeywordInfoManager: IDomainService
    {
        private readonly IRepository<KeywordInfo, Guid> _repositoryKeyword;

        public KeywordInfoManager(IRepository<KeywordInfo, Guid> repositoryKeyword)
        {
            _repositoryKeyword = repositoryKeyword;

        }
        /// <summary>
        /// 创建一个关键词
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Guid> CreateKeyword(KeywordInfo model)
        {
            var data = _repositoryKeyword.GetAll().Where(o=>o.KeywordTitle==model.KeywordTitle).FirstOrDefault();
            if (data != null)
                throw new UserFriendlyException("该关键字已存在！");

            return await _repositoryKeyword.InsertAndGetIdAsync(model);
        }
        /// <summary>
        /// 编辑一个关键词
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task UpdateKeyword(KeywordInfo model)
        {
            await _repositoryKeyword.UpdateAsync(model);
        }
        /// <summary>
        /// 删除一个关键词
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task DeleteKeyword(KeywordInfo model)
        {
            await _repositoryKeyword.DeleteAsync(model);
        }
    }
}
